/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import org.evswork.getwit.smackwrap.Talk;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.ClipboardManager;
import android.text.util.Linkify;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class TweetPageWrapper implements OnClickListener {
	GttMainActivity act;
	ViewGroup TweetLayout;
	
	TweetView TweetPage;
	
	ImageButton RTImBtn,QTImBtn;
	ImageButton ReplyImBtn,ReplyAllImBtn;
	ImageButton MoreImBtn,CloseImBtn;
	
	TweetPageWrapper(GttMainActivity act) {
		this.act = act;
		bindView();
	}

	private void bindView() {
		TweetLayout = (ViewGroup)act.findViewById(R.id.main_tweet_layout);
		TweetPage = (TweetView)TweetLayout.findViewById(R.id.tweet_page);
		// set linkify here because web-link breaks click event.
		TweetPage.MsgText.setAutoLinkMask(Linkify.WEB_URLS);
		
		RTImBtn = (ImageButton)TweetLayout.findViewById(R.id.rt_t_btn);
		QTImBtn = (ImageButton)TweetLayout.findViewById(R.id.qt_t_btn);
		ReplyImBtn = (ImageButton)TweetLayout.findViewById(R.id.rpy_t_btn);
		ReplyAllImBtn = (ImageButton)TweetLayout.findViewById(R.id.rpya_t_btn);
		MoreImBtn = (ImageButton)TweetLayout.findViewById(R.id.more_t_btn);
		CloseImBtn = (ImageButton)TweetLayout.findViewById(R.id.close_t_btn);
		
		RTImBtn.setOnClickListener(this);
		QTImBtn.setOnClickListener(this);
		ReplyImBtn.setOnClickListener(this);
		ReplyAllImBtn.setOnClickListener(this);
		MoreImBtn.setOnClickListener(this);
		CloseImBtn.setOnClickListener(this);
	}

	
	public void show() {
		TweetLayout.setVisibility(View.VISIBLE);
	}
	
	public void hide() {
		TweetLayout.setVisibility(View.INVISIBLE);
	}

	public void update(TweetObject data) {
		TweetPage.update(data);
		TweetLayout.setBackgroundDrawable(TweetPage.getBackground());
		TweetPage.setBackgroundResource(0);
	}
	
	@Override
	public void onClick(View v) {
		// all Builder should handle layout visibility control
		switch (v.getId()) {
		case R.id.rt_t_btn:
			Talk.send(String.format("-r %d", TweetPage.data.id));
			act.showHomeLayout();
			return;
		case R.id.qt_t_btn:
			//act.edWrapper.setMsg(String.format("-rt %d %s", TweetPage.data.id,act.edWrapper.getMsg()));
			act.edWrapper.RefView.update(TweetPage.data, RefTweetView.MODE_RT);
			act.showEditLayout(EditPageWrapper.FROM_TWEET,true);
			
			return;
		case R.id.rpy_t_btn:
			/*
			act.edWrapper.setMsg(
				String.format("-@ %d %s",
					TweetPage.data.id,
					act.edWrapper.getMsg())
			);*/
			act.edWrapper.RefView.update(TweetPage.data, RefTweetView.MODE_RPY_MSG);
			act.showEditLayout(EditPageWrapper.FROM_TWEET,true);
			
			return;
		case R.id.rpya_t_btn:
			new ReplyToDialogBuilder(
					act,
					TweetPage.data).DoReply();
			//act.showHomeLayout();
			return;
		case R.id.more_t_btn:
			new AlertDialog.Builder(act)
				.setTitle("其他操作:")
				.setItems(act.getResources().getStringArray(R.array.tweet_more_op),
					new DialogInterface.OnClickListener() {
						
		@Override
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case 0://msg
				Talk.send(String.format("-msg %d",TweetPage.data.id));
				act.showHomeLayout();
				break;
			case 1://st
				new RequestStatusDialogBuilder(act,TweetPage.data).DoWork();
				break;
			case 2://dm
				//act.edWrapper.setMsg(String.format("-d %s %s", TweetPage.data.user,act.edWrapper.getMsg()));
				act.edWrapper.RefView.update(TweetPage.data, RefTweetView.MODE_DM_MSG);
				act.showEditLayout(EditPageWrapper.FROM_TWEET,true);
				break;
			case 3://fav
				Talk.send(
						String.format("-fav %d",
							TweetPage.data.id)
						);
				break;
			case 4://unfav
				Talk.send(
						String.format("-unfav %d",
							TweetPage.data.id)
						);
				break;
			case 5://del
				Talk.send(String.format("-del %d",TweetPage.data.id));
				act.showHomeLayout();
				break;
			case 6:// copy tweet content
				{
					ClipboardManager cm = 
						(ClipboardManager)act.getSystemService(Activity.CLIPBOARD_SERVICE);
					cm.setText(TweetPage.data.content);
				}
				break;
			case 7:// copy tweet id
				{
					ClipboardManager cm = 
						(ClipboardManager)act.getSystemService(Activity.CLIPBOARD_SERVICE);
					cm.setText(String.valueOf(TweetPage.data.id));
				}
				break;
				
			}
			dialog.dismiss();
		}
				})
				.show();
			return;
		case R.id.close_t_btn:
			act.showHomeLayout();
			return;
		}
	
	}

}
